// 数组转树结构
export function arrayToTree(arr) {
    if (!arr) return [];
    // 根据parentId找到tree，不用再去遍历
    const idToTreeNode = new Map();
    let result = [];
    arr.forEach((item) => {
        const { menuId, parentId } = item;
        const treeNode = { ...item };
        idToTreeNode.set(menuId, treeNode);

        const parentNode = idToTreeNode.get(parentId);
        // 0代表无父节点
        if (parentNode) {
            parentNode.children.push(treeNode);
        }
        if (parentId === 0) result.push(treeNode);
    });
    return result;
}

// 节流函数
export function throttle(fn, delay) {
    let timer = null;
    return function () {
        if (timer) return;
        timer = setTimeout(() => {
            fn.apply(this, arguments);
            clearTimeout(timer);
            timer = null;
        }, delay);
    };
}

export function handleDate(time) {
    if (!time) return "";
    const date = time.split(" ")[0];
    if (date) {
        const arr = date.split("-");
        const year = arr[0];
        const month = arr[1];
        const day = arr[2];
        return [year, `${month}-${day}`, day, `${year}.${month}`, date];
    }
    return "--";
}
